Places and pathways
# Get Places data
t <- "20201216_104624"
data.places.physical <- readRDS(paste("data/places.physical_" , t, ".rds"))
# Define bounding box for our region of New Haven (will be the default zoom
# level of our visualization), but also define a slightly larger bounding box
# to give some more border in case viewers want to zoom out.
x.lim0 <- c(-72.939, -72.903)
y.lim0 <- c(41.302, 41.332)
x.lim <- x.lim0 + 0.04 * c(-1, 1)
y.lim <- y.lim0 + 0.02 * c(-1, 1)
p_jitter <- data.places.physical %>%
st_crop(xmin=x.lim[1], xmax=x.lim[2], ymin=y.lim[1], ymax=y.lim[2]) %>%
st_jitter()
p_jitter['x'] <- map_dbl(p_jitter$geometry, ~.[1])
p_jitter['y'] <- map_dbl(p_jitter$geometry, ~.[2])
# Get map data
# newhaven_lowres <- get_stamenmap(bbox=c(left=x.lim[1], right=x.lim[2], bottom=y.lim[1], top=y.lim[2]), zoom=15)
# newhaven <- get_stamenmap(bbox=c(left=x.lim0[1], right=x.lim0[2], bottom=y.lim0[1], top=y.lim0[2]), zoom=17)
# saveRDS(newhaven_lowres, "data/newhaven_lowres.rds", compress=F)
# saveRDS(newhaven, "data/newhaven.rds", compress=F)
newhaven_lowres <- readRDS("data/newhaven_lowres.rds")
newhaven <- readRDS("data/newhaven.rds")
Simplefeatures are great, but we actually end up using rasters. This allows the viewer to see precise building outlines on the map (while remaining feasible to render and interact with on a normal computer/browser). In addition, geom_density_2d() needs x,y data rather than simplefeatures.1
g <- ggmap(newhaven_lowres) +
inset_ggmap(newhaven) +
geom_density_2d(data=p_jitter, aes(x=x, y=y), size=0.4, alpha=0.8, bins=15) + # Sadly, plotly isn't ready for density_2d_filled
geom_point(data=p_jitter, aes(x=x, y=y), color="yellow", alpha=0.9, size=0.6) +
lims(x=x.lim0, y=y.lim0) +
theme(axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank())
ggplotly(g)